﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace CentralOffice.WebClientTest
{
    /// <summary>
    /// NoLoginService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class NoLoginService : System.Web.Services.WebService
    {
        private const string Select_App = "SELECT top 20 Id, ApplicationId, ApprovedBy, DateApproved, ContentText, Opinion FROM Tasks.Approvals order by ModifiedTime desc";
        private const string Select_Log = "SELECT top 20 LogID, Message, FormattedMessage FROM Admin.Log  ORDER BY Timestamp DESC ";
        private const string Select_Task = "SELECT top 20 Id, Name FROM Tasks.Tasks order by ModifiedTime desc";



        [WebMethod]
        public DataSet GetData(string sql)
        {
            return HandleCommand(sql, null);
        }

        [WebMethod]
        public DataSet GetMultiTable(string sql, string tableName)
        {
            return HandleCommand(sql, tableName);
        }

        private DataSet HandleCommand(string sql, string tableName)
        {
            System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
            System.Configuration.ConnectionStringSettings cs = rootWebConfig.ConnectionStrings.ConnectionStrings["CentralOfficeConnectionString"];

            SqlConnection cn = new SqlConnection(cs.ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();

            string t = tableName;
            if (t == null)
            {
                t = "Table";
            }

            try
            {
                cn.Open();
                adapter.Fill(ds, t);
            }
            catch
            {
                throw;
            }
            finally
            {
                cn.Close();
            }
            return ds;
        }

        private static DataTable ExecuteDataTable(string sql)
        {
            DataTable dt = null;
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            try
            {
                DataSet ds = db.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch
            {
                throw;
            }
            finally
            {

            }
            return dt;
        }

        [WebMethod]
        public DataTable GetDataTable(string sql)
        {
            return ExecuteDataTable(sql);
        }

        [WebMethod]
        public DataTable GetLogTable()
        {
            return ExecuteDataTable(Select_Log);
        }

        [WebMethod]
        public DataTable GetAppTable()
        {
            return ExecuteDataTable(Select_App);
        }

        [WebMethod]
        public DataTable GetTaskTable()
        {
            return ExecuteDataTable(Select_Task);
        }
    }
}
